package com.zb.zeus.common.security.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Maps;
import com.zb.zeus.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author fujianjian
 * @date 2021/9/16 10:49
 **/
@Slf4j
@Component
public class String2MapConverter implements Converter<String, Map> {
    @Override
    public Map convert(String s) {
        Map map = Maps.newLinkedHashMap();
        if (StringUtils.isNotEmpty(s)) {
            try {
                map = JSON.parseObject(s, new TypeReference<Map<String,Object>>(){}.getType());
            } catch (Exception e) {
                log.error("string to map error:{}", s, e);
            }
        }
        return map;
    }
}
